define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 'shiftform', 'modalSearch',
        'datetimepicker', 'datetimepickerCN'],
function($, validate) {
  return {
    
    init: function() {
      this.initPage();
      this.initWarehouse();
      this.initCompany();
      this.initVehicle();
    },
    
    initPage: function(){
        $('.btn-remove').on('click', function(e){
            $('input').val('');
        });
        
        $(document).find('form').bt_validate();
        
        $('select[name="organizationIds"]').select2({
          theme: "classic",
          placeholder : '请选择',
          minimumResultsForSearch : -1,
          allowClear : true,
        });
        
        $('select[name="transType"]').select2({
          theme: "classic",
          placeholder : '请选择',
          minimumResultsForSearch : -1,
          allowClear : true,
        });
      
        $('select[name="status"]').select2({
          theme: "classic",
          placeholder : '请选择',
          minimumResultsForSearch : -1,
          allowClear : true,
        });
      
        $('.date').datetimepicker({
          format: 'yyyy-mm-dd',
          autoclose: true,
          language: 'zh-CN',
          todayBtn: 'linked',
          startDate: '${today}',
          todayHighlight: true,
          pickerPosition: 'bottom-right',
          minView: 'month'
        });
    },
    
    initWarehouse: function() {
        
        $('[data-target="#modal-search-fromWarehouse"]').modalsearch({
          url: window.__CTX__.root + '/warehouses/query',
          tableColumns: ['出发地代码', '出发地名称'],
          tableValues: ['code', 'name'],
          displayValues: ['code', 'name'],
          values: ['id']
        });
        
        $('[data-target="#modal-search-toWarehouse"]').modalsearch({
          url: window.__CTX__.root + '/warehouses/query',
          tableColumns: ['到达地代码', '到达地名称'],
          tableValues: ['code', 'name'],
          displayValues: ['code', 'name'],
          values: ['id']
        });
        
        var options = {
          time: 1000,
          url: window.__CTX__.root + "/warehouses/query",
          queryName: "code",
          value: ["id"],
          display: ["code", "name"]
        }
        
        $('.fromWarehouse-search').koalaSearch(options);
        $('.toWarehouse-search').koalaSearch(options);
        
        $(document).on("koala.search.value.changed", ".fromWarehouse-search, .toWarehouse-search", function(){
          console.log($(this).next().val());
          // TODO 其他查询后的逻辑
        })
    },
    
    initVehicle:function() {
        
      $('[data-target="#modal-search-vehicle"]').modalsearch({
          url: function(){
                var url = window.__CTX__.root + '/vehicles/query';
                return url + "?companyIds=" + $("select[name='organizationIds']").val();
             },
            tableColumns: ['驳运车代码', '驳运车名称'],
            tableValues: ['code', 'name'],
            displayValues: ['code', 'name'],
            values: ['id']
      });
        
      var options = {
        time: 1000,
        url: window.__CTX__.root + "/vehicles/query",
        queryName: "code",
        value: ["id"],
        display: ["code", "name"]
      }
      
      $('.vehicle-search').koalaSearch(options);
    },
        
    initCompany:function() {
        
    	$('[data-target="#modal-search-company"]').modalsearch({
            url: window.__CTX__.root + '/transportation-companys/query',
            tableColumns: ['运输公司代码', '运输公司名称'],
            tableValues: ['code', 'name'],
            displayValues: ['code', 'name'],
            values: ['id']
      });
      
       
       var options = {
         time: 1000,
         url: window.__CTX__.root + "/transportation-companys/query",
         queryName: "code",
         value: ["id"],
         display: ["code", "name"]
       }
     
       $('.company-search').koalaSearch(options);
       }
  }
})